#include <Def.h>
#include "Property.h"

Property Property::fromJson(const QJsonObject& object)
{
	QString name = object["name"].toString();
	QString type = object["type"].toString();
	QString description = object["description"].toString();
	if(type == "int")
		return Property(object["value"].toInt(),name,description);
	else if(type=="double")
		return Property(object["value"].toDouble(),name,description);
	if(type=="boolean")
		return Property(object["value"].toBool(),name,description);
	if(type=="string")
		return Property(object["value"].toString(),name,description);
	return Property();
}

Property::Property()
{
}

Property::Property(const Property& other):
valueInt(other.valueInt),
valueDouble(other.valueDouble),
valueBoolean(other.valueBoolean),
valueString(other.valueString),
valueType(other.valueType),
valueName(other.valueName),
valueDescription(other.valueDescription)
{
}

Property::Property(const char* value,const QString& name,const QString& desc):
valueString(value),
valueType(TypeString),
valueName(name),
valueDescription(desc)
{
}

Property::Property(int value,const QString& name,const QString& desc):
valueInt(value),
valueType(TypeInt),
valueName(name),
valueDescription(desc)
{
}

Property::Property(double value,const QString& name,const QString& desc):
valueDouble(value),
valueType(TypeDouble),
valueName(name),
valueDescription(desc)
{
}

Property::Property(bool value,const QString& name,const QString& desc):
valueBoolean(value),
valueType(TypeBoolean),
valueName(name),
valueDescription(desc)
{
}

Property::Property(const QString& value,const QString& name,const QString& desc):
valueString(value),
valueType(TypeString),
valueName(name),
valueDescription(desc)
{
}

Property::~Property()
{
}

int Property::asInt()const
{
	if(valueType == TypeInt)
		return valueInt;
	return valueString.toInt();
}

double Property::asDouble()const
{
	if(valueType == TypeDouble)
		return valueDouble;
	return valueString.toDouble();
}

bool Property::asBoolean()const
{
	if(valueType == TypeBoolean)
		return valueBoolean;
	return valueString == "True" ||
		valueString == "true" ||
		valueString == "1";
}

QString Property::getString()const
{
	if(valueType == TypeString)
		return valueString;
	
	QString ret;
	if(valueType == TypeBoolean)
		ret = valueBoolean ? "True" : "False";
	else if(valueType != TypeNill)
		ret = QString("%1").arg(valueInt);
	return ret;
}

QJsonObject Property::toJson()const
{
    QJsonObject object;
	object.insert("name",getName());
	object.insert("description",getDesciption());
	object.insert("type",getValueType());
	
	if(valueType ==TypeInt)
		object.insert("value",valueInt);
	else if(valueType == TypeDouble)
		object.insert("value",valueDouble);
	else if(valueType == TypeBoolean)
		object.insert("value",valueBoolean);
	else if(valueType == TypeString)
		object.insert("value",valueString);
	return object;
}